/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.robot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.excel.annotation.ResponseExcel;
import com.pig4cloud.pigx.common.log.annotation.SysLog;

import com.pig4cloud.pigx.robot.entity.RobotConfig;
import com.pig4cloud.pigx.robot.rabbitmq.MQConstant;
import com.pig4cloud.pigx.robot.service.RobotConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 机器人配置表
 *
 * @date 2022-08-11 10:43:17
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/robotconfig")
@Api(value = "robotconfig", tags = "机器人配置表管理")
public class RobotConfigController {

	private final RobotConfigService robotConfigService;
	@Autowired
	private RabbitTemplate rabbitTemplate;
    /**
     * 分页查询
     * @param page 分页对象
     * @param robotConfig 机器人配置表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('robot_robotconfig_view')" )
    public R getRobotConfigPage(Page page, RobotConfig robotConfig) {
		robotConfig.setIsDeleted("0");
		return R.ok(robotConfigService.page(page, Wrappers.query(robotConfig)));
	}


	/**
	 * 通过id查询机器人配置表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('robot_robotconfig_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(robotConfigService.getById(id));
	}

	/**
	 * 新增机器人配置表
	 *
	 * @param robotConfig 机器人配置表
	 * @return R
	 */
	@ApiOperation(value = "新增机器人配置表", notes = "新增机器人配置表")
	@SysLog("新增机器人配置表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('robot_robotconfig_add')")
	public R save(@RequestBody RobotConfig robotConfig) {
		robotConfig.setIsDeleted("0");
		BaseMapper<RobotConfig> mapper = robotConfigService.getBaseMapper();
		QueryWrapper<RobotConfig> wrapper = new QueryWrapper<>();
		wrapper.eq("symbol_id", robotConfig.getSymbolId());
		wrapper.eq("is_deleted", "0");
		Long count = mapper.selectCount(wrapper);
		if (count > 0) {
			return R.failed("该币对不可重复配置机器人");
		}
		boolean rst = robotConfigService.save(robotConfig);
		rabbitTemplate.convertAndSend(MQConstant.EXCHANGE_FANOUT_ROBOT_SYMBOL_UPDATE, "", rst);
		return R.ok(rst);
	}

	/**
	 * 修改机器人配置表
	 *
	 * @param robotConfig 机器人配置表
	 * @return R
	 */
	@ApiOperation(value = "修改机器人配置表", notes = "修改机器人配置表")
	@SysLog("修改机器人配置表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('robot_robotconfig_edit')")
	public R updateById(@RequestBody RobotConfig robotConfig) {
		robotConfig.setIsDeleted("0");
		BaseMapper<RobotConfig> mapper = robotConfigService.getBaseMapper();
		QueryWrapper<RobotConfig> wrapper = new QueryWrapper<>();
		wrapper.eq("symbol_id", robotConfig.getSymbolId());
		wrapper.ne("id", robotConfig.getId());
		wrapper.eq("is_deleted", "0");
		Long count = mapper.selectCount(wrapper);
		if (count > 0) {
			return R.failed("该币对不可重复配置机器人");
		}
		boolean rst = robotConfigService.updateById(robotConfig);
		rabbitTemplate.convertAndSend(MQConstant.EXCHANGE_FANOUT_ROBOT_SYMBOL_UPDATE, "", rst);
		return R.ok(rst);
	}

	/**
	 * 通过id删除机器人配置表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除机器人配置表", notes = "通过id删除机器人配置表")
	@SysLog("通过id删除机器人配置表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('robot_robotconfig_del')")
	public R removeById(@PathVariable Long id) {
		RobotConfig config = new RobotConfig();
		config.setId(id);


		boolean rst =robotConfigService.updateById(config);
		rabbitTemplate.convertAndSend(MQConstant.EXCHANGE_FANOUT_ROBOT_SYMBOL_UPDATE,"",rst);
		return R.ok(rst);
	}


	/**
	 * 导出excel 表格
	 *
	 * @param robotConfig 查询条件
	 * @return excel 文件流
	 */

	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('robot_robotconfig_export')")
	public List<RobotConfig> export(RobotConfig robotConfig) {
		return robotConfigService.list(Wrappers.query(robotConfig));
	}
}
